package com.shenyue.nbtownlet.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.databinding.ProjectManagementListItemBinding;
import com.shenyue.nbtownlet.util.ColorUtil;
import com.shenyue.nbtownlet.util.StringUtil;

public class ProjectManagementListViewAdapter extends BaseAdapter {
    private Context mContext;
    private JSONArray items = new JSONArray();
    public interface ProjectManagementListItemClickListener{
        void onItemClick(int position);
    }
    private ProjectManagementListItemClickListener listener;

    public ProjectManagementListViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            ProjectManagementListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.project_management_list_item, viewGroup, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ProjectManagementListItemBinding root = viewHolder.getBinding();
        root.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onItemClick(position);
            }
        });
        JSONObject item = items.getJSONObject(position);
        root.NAME.setText(StringUtil.getValue(item.getString("NAME")));
        root.TYPENAME.setText(StringUtil.getValue(item.getString("TYPENAME")));
        root.AREANAMESTREETNAME.setText(StringUtil.getValue(item.getString("AREANAME")) + "\u3000" + StringUtil.getValue(item.getString("STREETNAME")));
        String SEQ = StringUtil.getValue(item.getString("SEQ"));
        if(StringUtil.isNull(SEQ))
            SEQ = "0";
        root.SEQ.setText(SEQ + "%");
        return convertView;
    }

    public void addItems(JSONArray array) {
        items.addAll(array);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(ProjectManagementListItemClickListener listener){
        this.listener = listener;
    }

    private class ViewHolder {
        private ProjectManagementListItemBinding binding;

        public ViewHolder(ProjectManagementListItemBinding binding) {
            this.binding = binding;
        }

        public ProjectManagementListItemBinding getBinding() {
            return binding;
        }
    }
}
